﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;

namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblMediaRoomDAL:ItblMediaRoom
    {
        HKEA_CMSEntities model;
        tblMediaRoom mediaRoom;

        #region 构造函数

        public tblMediaRoomDAL()
        {
            model = DataAccess.GetModelInstance();
            mediaRoom = model.tblMediaRooms.CreateObject();
        }
        public static tblMediaRoomDAL GetInstance()
        {
            return new tblMediaRoomDAL();
        }

        #endregion

        #region 公共函数

        public tblMediaRoom GetEntity(int uid)
        {
            return model.tblMediaRooms.Where(a => a.MediaRoomUID == uid).SingleOrDefault();
        }

        /// <summary>
        /// 获取最后一条记录，特殊用途
        /// </summary>
        /// <returns></returns>
        public tblMediaRoom GetLastEntity()
        {
            return model.tblMediaRooms.Where(a => a.MediaRoomUID >0).OrderByDescending(a=>a.CreatedAt).FirstOrDefault();
        }
        public List<MediaRoomList> GetList(tblMediaRoom ent,List<int> WithOutList)
        {
            var ret = from room in model.tblMediaRooms
                      select new MediaRoomList()
                      {
                          AppKeyUID = 0,
                          CateGory = room.Category,
                          CustomerUID = (int)room.CustomerUID,
                          FileName = room.FileName,
                          FilePath = room.FilePath,
                          From = "tbl",
                          KeyUID = room.MediaRoomUID,
                          MediaType = room.MediaType,
                          TblKeyUID = (int)room.MediaRoomUID
                      };
            if (ent.MediaRoomUID > 0)
                ret = ret.Where(a => a.TblKeyUID == ent.MediaRoomUID);
            if (ent.CustomerUID > 0)
                ret = ret.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.MediaRoomUID > 0)
                ret = ret.Where(a => a.TblKeyUID == ent.MediaRoomUID);
            if (ent.CustomerUID > 0)
                ret = ret.Where(a=>a.CustomerUID==ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.Category))
                ret = ret.Where(a => a.CateGory == ent.Category);
            if (!string.IsNullOrEmpty(ent.MediaType))
                ret = ret.Where(a => a.MediaType == ent.MediaType);
            if (WithOutList!=null && WithOutList.Count > 0)
                ret = ret.Where(a=>!WithOutList.Contains(a.TblKeyUID));
            return ret.OrderByDescending(a=>a.TblKeyUID).ToList();
        }

        public List<tblMediaRoom> GetListByENT(tblMediaRoom ent)
        {
            var result = model.tblMediaRooms.Where(a => a.MediaRoomUID > 0);
            if (!string.IsNullOrEmpty(ent.MediaType ))
                result = result.Where(a => a.MediaType == ent.MediaType);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.FileAlt_Eng))
                result = result.Where(a => a.FileAlt_Eng == ent.FileAlt_Eng);
            if (!string.IsNullOrEmpty(ent.FileAlt_Chi))
                result = result.Where(a => a.FileAlt_Chi == ent.FileAlt_Chi);
            if (!string.IsNullOrEmpty(ent.Des_Eng))
                result = result.Where(a => a.Des_Eng == ent.Des_Eng);
            if (ent.IsPublish>=0)
                result = result.Where(a => a.IsPublish == ent.IsPublish);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.OrderByDescending(a => a.CreatedAt).ToList();
        }

        public List<tblMediaRoom> GetListByENT(tblMediaRoom ent, PageParam param, string strPropertyName, string strOrderBy)
        {
            var result = model.tblMediaRooms.Where(a => a.MediaRoomUID > 0);
            if (!string.IsNullOrEmpty(ent.MediaType))
                result = result.Where(a => a.MediaType == ent.MediaType);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.CustomerUID>0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.FileAlt_Eng))
                result = result.Where(a => a.FileAlt_Eng == ent.FileAlt_Eng);
            if (!string.IsNullOrEmpty(ent.FileAlt_Chi))
                result = result.Where(a => a.FileAlt_Chi == ent.FileAlt_Chi);
            if (!string.IsNullOrEmpty(ent.Des_Eng))
                result = result.Where(a => a.Des_Eng == ent.Des_Eng);
            if (ent.IsPublish >= 0)
                result = result.Where(a => a.IsPublish == ent.IsPublish);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip(param.RecordStart).Take(param.PageSize);
            return result.ToList();
        }

        public int GetCountByENT(tblMediaRoom ent)
        {
            var result = model.tblMediaRooms.Where(a => a.MediaRoomUID > 0);
            if (!string.IsNullOrEmpty(ent.MediaType))
                result = result.Where(a => a.MediaType == ent.MediaType);
            if (!string.IsNullOrEmpty(ent.Category))
                result = result.Where(a => a.Category == ent.Category);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.FileAlt_Eng))
                result = result.Where(a => a.FileAlt_Eng == ent.FileAlt_Eng);
            if (!string.IsNullOrEmpty(ent.FileAlt_Chi))
                result = result.Where(a => a.FileAlt_Chi == ent.FileAlt_Chi);
            if (!string.IsNullOrEmpty(ent.Des_Eng))
                result = result.Where(a => a.Des_Eng == ent.Des_Eng);
            if (ent.IsPublish >= 0)
                result = result.Where(a => a.IsPublish == ent.IsPublish);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int InsertENT(tblMediaRoom ent)
        {
            ent.CreatedAt = DateTime.Now;
            ent.UpdatedAt = DateTime.Now;
            model.tblMediaRooms.AddObject(ent);
            model.SaveChanges();

            ent = model.tblMediaRooms.Where(a => a.MediaRoomUID > 0).OrderBy(a => a.MediaRoomUID).ToList().Last();
            return ent.MediaRoomUID;
        }

        public int UpdateENT(tblMediaRoom ent)
        {
            mediaRoom = model.tblMediaRooms.Where(a=>a.MediaRoomUID==ent.MediaRoomUID).FirstOrDefault();
            if (mediaRoom != null)
            {
                mediaRoom.MediaType = ent.MediaType;
                mediaRoom.Category = ent.Category;
                mediaRoom.CustomerUID = ent.CustomerUID;
                mediaRoom.AliasName = ent.AliasName;
                mediaRoom.FileName = ent.FileName;
                mediaRoom.FilePath = ent.FilePath;
                mediaRoom.FileExt = ent.FileExt;
                mediaRoom.FileAlt_Eng = ent.FileAlt_Eng;
                mediaRoom.FileAlt_Chi = ent.FileAlt_Chi;
                mediaRoom.FileLink_Eng = ent.FileLink_Eng;
                mediaRoom.FileLink_Eng = ent.FileLink_Eng;
                mediaRoom.Des_Eng = ent.Des_Eng;
                mediaRoom.Des_Chi = ent.Des_Chi;
                mediaRoom.IsPublish = ent.IsPublish;
                mediaRoom.LastModifyBy = ent.LastModifyBy;
                mediaRoom.UpdatedAt = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblMediaRoom ent)
        {
            mediaRoom = model.tblMediaRooms.Where(a => a.MediaRoomUID == ent.MediaRoomUID).FirstOrDefault();
            if (mediaRoom != null)
            {
                model.tblMediaRooms.DeleteObject(mediaRoom);
            }
            return model.SaveChanges();
        }

        #endregion 
    }
}
